package com.zlfund.cat.zjgsmk.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup;

import com.zlfund.cat.zjgsmk.R;

/**
 * Created by chenj
 * Created on 2017/8/7
 */

public class RadioButton extends android.support.v7.widget.AppCompatRadioButton {
    private float mDrawableHeight;      //图片高度
    private float mDrawableWidth;       //图片宽度
    private Drawable mTextDrawable;      //图片源
    private float mDrawablePaddingTop, mDrawablePaddingBottom;  //图片距离底部文字和顶部的距离
    private float mExtraHeight;          //图片部分的高度(加padding值)
    private float mHeight, mWidth;       //控件宽高
    private TextPaint mPaint;              //底部文字画笔
    private CharSequence mText;            //底部文字
    private int mSignWidth,mSignHeight;  //标记宽高(用作适配)
    private ViewGroup.LayoutParams mLayoutParams;
    private ColorStateList mTextColor;
    private int mCurColor;

    public RadioButton(Context context) {
        this(context, null);
    }

    public RadioButton(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.radioButtonStyle);
    }

    public RadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RadioButton);
        final Drawable drawable = a.getDrawable(R.styleable.RadioButton_text_drawable);
        float padding = a.getDimension(R.styleable.RadioButton_drawablePadding, 0);
        mPaint = getPaint();
        mDrawablePaddingTop = a.getDimension(R.styleable.RadioButton_drawablePaddingTop, padding);
        mDrawablePaddingBottom = a.getDimension(R.styleable.RadioButton_drawablePaddingBottom, padding);
        mDrawableHeight = a.getDimension(R.styleable.RadioButton_drawableHeight, 0);
        mDrawableWidth = a.getDimension(R.styleable.RadioButton_drawableWidth, 0);
        mSignWidth = a.getInt(R.styleable.RadioButton_screenWidth, 0);
        mSignHeight = a.getInt(R.styleable.RadioButton_screenHeight, 0);
        a.recycle();
        if (mSignWidth != 0) {
            final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
            float scale = screenWidth * 1.0f / mSignWidth;
            mDrawablePaddingTop *= scale;
            mDrawablePaddingBottom *= scale;
            mDrawableHeight *= scale;
            mDrawableWidth *= scale;
        }
        mTextDrawable = drawable;
        if (mTextDrawable != null) {
            //通过这个Callback,在状态改变的时候可以产生回调刷新页面
            mTextDrawable.setCallback(this);
        }
        mExtraHeight = mDrawableHeight + mDrawablePaddingBottom + mDrawablePaddingTop;
        mTextColor = getTextColors();
        mCurColor = mTextColor.getDefaultColor();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mLayoutParams = getLayoutParams();
        if (getMeasuredHeight() < getTextHeight(mPaint) + mExtraHeight + getPaddingBottom() + getPaddingTop())
            //设置控件宽高(测量高度加上图片部分高度)
            setMeasuredDimension(getMeasuredWidth(), (int) (getTextHeight(mPaint) + mExtraHeight + getPaddingBottom() + getPaddingTop()));
    }

    private float getTextHeight(Paint paint) {
        return paint.descent() - paint.ascent();
    }

    //修改图片的状态(state_checked)
    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();

        Drawable d = mTextDrawable;
        int[] ints = getDrawableState();
        if (d != null){
            d.setState(ints);
        }

        if (mTextColor != null && mTextColor.isStateful()) {
            updateTextColors();
        }
    }

    private void updateTextColors() {
        int color = mTextColor.getColorForState(getDrawableState(), 0);
        if (mCurColor != color){
            mCurColor = color;
            invalidate();
        }
    }

    protected boolean verifyDrawable(@NonNull Drawable who) {
        return who == mTextDrawable || super.verifyDrawable(who);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        mPaint.setColor(mCurColor);
        if (mWidth == 0) {
            mWidth = getWidth();
            mHeight = getHeight();
        }
        if (mTextDrawable != null) {
            int left = 0;
            int top = (int) (mDrawablePaddingTop + getPaddingTop());
            if (mDrawableWidth < mWidth) {
                left = (int) ((mWidth - mDrawableHeight) / 2);
            }
            //设置drawable区域
            mTextDrawable.setBounds(left, top, (int) (left + mDrawableWidth), (int) (top + mDrawableHeight));
            mTextDrawable.draw(canvas);
        }
        mText = getText();
        float textWidth = mPaint.measureText(mText, 0, mText.length());
        canvas.drawText(mText, 0, mText.length(), (mWidth - textWidth) / 2, mHeight - getPaddingBottom(), mPaint);
    }
}
